package com.ncu.musicbox2020;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.core.app.NotificationCompat;

public class MusicPlayerService extends Service {
    private static final String TAG = MusicPlayerService.class.getSimpleName();
    private static final int REQUEST_CODE = 100;
    private static final String ACTION_PLAY = "play";
    private static final String ACTION_LAST = "last";
    private static final String ACTION_NEXT = "next";
    PlayerReceiver playerReceiver;
    public MusicPlayerService() {
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public void onCreate(){
        super.onCreate();
        playerReceiver = new PlayerReceiver();
        IntentFilter mFilter = new IntentFilter();

        mFilter.addAction(ACTION_PLAY);
        mFilter.addAction(ACTION_LAST);
        mFilter.addAction(ACTION_NEXT);

        registerReceiver(playerReceiver, mFilter);
    }
    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy()");
        stopForeground(true);// 停止前台服务--参数：表示是否移除之前的通知
        if (playerReceiver != null)
        unregisterReceiver(playerReceiver);// 解除广播注册
        super.onDestroy();
    }
    @Override
    public int  onStartCommand(Intent intent, int flags, int startId){
        RemoteViews remoteViews = new RemoteViews(this.getPackageName(),R.layout.notifytion);

        Notification notification = new NotificationCompat.Builder(this)
                .setContent(remoteViews)
                .setSmallIcon(R.drawable.music)
                .setWhen(System.currentTimeMillis())
                .build();
        notification.defaults = Notification.DEFAULT_SOUND;
        startForeground(1,notification);

        Intent intentPlay = new Intent(ACTION_PLAY);// 指定操作意图--设置对应的行为ACTION
        PendingIntent pIntentPlay = PendingIntent.getBroadcast(this.getApplicationContext(),
                REQUEST_CODE, intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);// 取的一个PendingIntent，
        remoteViews.setOnClickPendingIntent(R.id.btn_play1, pIntentPlay);

        Intent intentNext = new Intent(ACTION_NEXT);// 指定操作意图--设置对应的行为ACTION
        PendingIntent pIntentNext = PendingIntent.getBroadcast(this.getApplicationContext(),
                REQUEST_CODE, intentNext, PendingIntent.FLAG_UPDATE_CURRENT);// 取的一个PendingIntent，
        remoteViews.setOnClickPendingIntent(R.id.btn_next1, pIntentNext);

        Intent intentPrev = new Intent(ACTION_LAST);// 指定操作意图--设置对应的行为ACTION
        PendingIntent pIntentPrev = PendingIntent.getBroadcast(this.getApplicationContext(),
                REQUEST_CODE, intentPrev, PendingIntent.FLAG_UPDATE_CURRENT);// 取的一个PendingIntent，
        remoteViews.setOnClickPendingIntent(R.id.btn_previous1, pIntentPrev);

        return super.onStartCommand(intent,flags,startId);
    }
}
